﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using WebReport.Models;
using Utility;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Resource;

namespace WebReport.Controllers
{
    public class ReportController: Controller
    {
		private readonly ILogger<ReportController> _logger;

		private readonly IConfiguration _configuration;

		public ReportController(ILogger<ReportController> logger, IConfiguration configuration)
		{
			_logger = logger;
			_configuration = configuration;
		}

		/// <summary>
		/// 机台产能统计
		/// </summary>
		/// <returns></returns>
		public IActionResult MachineSum()
		{
			return View();
		}

		/// <summary>
		/// 设备状态
		/// </summary>
		/// <returns></returns>
		public IActionResult DeviceStatus()
        {
			return View();
		}

		/// <summary>
		/// WIPReport
		/// </summary>
		/// <returns></returns>
		public IActionResult WIPReport()
		{
			return View();
		}
		/// <summary>
		/// 良率看板
		/// </summary>
		/// <returns></returns>
		public IActionResult Yieldboard()
		{
			return View();
		}

		/// <summary>
		/// 当天产能统计
		/// </summary>
		/// <returns></returns>
		public IActionResult DayOuput()
		{
			//base.ViewBag.Port = _configuration["Dashboard:WebAPI"];
			return View();
		}

		/// <summary>
		/// 昨天产能统计
		/// </summary>
		/// <returns></returns>
		public IActionResult YtdOuput()
		{
			return View();
		}

		public IActionResult EquipmentAdd()
		{
			return View();
		}

		/// <summary>
		/// 设备利用率
		/// </summary>
		/// <returns></returns>
		public IActionResult UtilizationRate()
		{
			return View();
		}

		/// <summary>
		/// OEE_Report
		/// </summary>
		/// <returns></returns>
		public IActionResult ReportOee()
		{
			return View();
		}


		/// <summary>
		/// 设备状态 旧
		/// </summary>
		/// <returns></returns>
		public IActionResult Equipment()
		{
			return View();
		}

		/// <summary>
		/// 良率报表
		/// </summary>
		/// <returns></returns>
		public IActionResult ReportYield()
		{
			return View();
		}

		/// <summary>
		/// 来料参数趋势
		/// </summary>
		/// <returns></returns>
		public IActionResult ToPnParmTrend()
		{
			return View();
		}

		//设备组装工序良率
		public IActionResult DeviceAssemblyYield()
		{
			return View();
		}

        //DeviceByTestYield
        public IActionResult DeviceByTestYield()
        {
            return View();
        }

        /// <summary>
        /// 单项测试分析图
        /// </summary>
        /// <returns></returns>
        public IActionResult SingleIterm()
		{
			return View();
		}

		/// <summary>
		/// 测试项 趋势图表
		/// </summary>
		/// <returns></returns>
		public IActionResult TestsTrend()
		{
			return View();
		}
		/// <summary>
		/// 良率看板 旧
		/// </summary>
		/// <returns></returns>
		public IActionResult Yield()
		{
			return View();
		}

		//测试工序产能
		public IActionResult TestProd()
		{
			return View();
		}

		//组装工序产能
		public IActionResult AssemblyProd()
		{
			return View();
		}

		//工单达标率
		public IActionResult JobNumReachRate()
		{
			return View();
		}

		//OEE报表
		public IActionResult OEEReport()
		{
			ViewBag.Address = _configuration["Dashboard:WebAPI"];
			return View();
		}

		//组装工序良率
		public IActionResult AssemblyYield()
		{
			return View();
		}

        //组装工序不良率
        public IActionResult AssemblyDefect()
        {
            return View();
        }

		//良率目标报表
        public IActionResult YieldTriggerReport()
        {
            return View();
        }

        //良率Failure
        public IActionResult YieldFailureMode()
        {
            return View();
        }

        //设备产出
        public IActionResult TesterProd()
		{
			return View();
		}

        //HpuByYieldReport
        public IActionResult HpuByYieldReport()
        {
            return View();
        }

        //HpuReport
        public IActionResult HpuReport()
        {
            return View();
        }

        //FailureModeBoxplot
        public IActionResult FailureModeBoxplot()
        {
            return View();
        }

        //Output Plan Comparison
        public IActionResult OutputPlanReport()
        {
            return View();
        }

        //Test Station Yield Analysis
        public IActionResult Tsya()
        {
            return View();
        }

        //LDUYield
        public IActionResult LDUYield()
        {
            return View();
        }

        //FYOEE
        public IActionResult FYOEE()
        {
            return View();
        }

        //FAInOutWipTrend
        public IActionResult FAInOutWipTrend()
        {
            return View();
        }

        //Material
        public IActionResult MaterialTransaction()
        {
            return View();
        }

        public string EquipmentData()
		{
            string response = HttpHelper.GetRequest(_configuration["Dashboard:EmsAPI"] + "/api/ems/getEquipmentsStatus","GET","application/json",100);
            List<Equipment> equipmentlist = GeneralMethod.JsonReverse<List<Equipment>>(response);
			var groupdata = (from equipment in equipmentlist
							 //where equipment.status_id != 5
							 group equipment by equipment.status_name into grouped
							 orderby grouped.Count()
							 select new
							 {
								 name = grouped.Key,
								 value = grouped.Count()
							 }).ToList();
			List<Equipment> list = equipmentlist.OrderBy((Equipment c) => c.status_id).ToList();
			List<Equipment> exception = (from t in equipmentlist
										 where t.status_id != 5
										 select t into c
										 orderby c.status_id
										 select c).ToList();
			decimal efficiency = Math.Round(Convert.ToDecimal(list.Count - exception.Count) / (decimal)list.Count * 100m, 2);
			Dictionary<string, object> data = new Dictionary<string, object>();
			data.Add("groupdata", groupdata);
			data.Add("list", list);
			data.Add("exception", exception);
			data.Add("efficiency", efficiency);
			return GeneralMethod.ToJson(data);
		}

		public List<Comboxlist> YieldComboBoxPrdData()
		{
			string connectionString = _configuration.GetConnectionString("ConnectionString");
			string response = HttpHelper.HttpPost(_configuration["Dashboard:WebAPI"] + "/Dashboard/GetYieldPrd", "POST", "application/json", string.Empty, 100);
			JObject parm = JObject.Parse(response);
			return JsonConvert.DeserializeObject<List<Comboxlist>>(parm["ReturnData"].ToString());
		}

		public List<Comboxlist> YieldComboBoxProData(string product_code)
		{
			string response = HttpHelper.HttpPost(_configuration["Dashboard:WebAPI"] + "/Dashboard/GetYieldPro?product_code=" + product_code, "POST", "application/json", string.Empty, 100);
			JObject parm = JObject.Parse(response);
			return JsonConvert.DeserializeObject<List<Comboxlist>>(parm["ReturnData"].ToString());
		}

		public YieldData Getdata(string proj_code, string process, string prodtype, string start, string end,string area)
		{
			YieldData yielddata = new YieldData();
			string response = HttpHelper.HttpPost(_configuration["Dashboard:WebAPI"] + @$"/ReportAction/GetYieldData?proj_code={proj_code}&process={process}&prodtype={prodtype}&start={start}&end={end}&area={area}", "POST", "application/json", string.Empty, 100);
			JObject parm = JObject.Parse(response);

            yielddata.yield = JsonConvert.DeserializeObject<List<YieldMoude>>(parm["ReturnData"]["Table"].ToString());
            yielddata.failuremodeplato = JsonConvert.DeserializeObject<List<FailureModePlato>>(parm["ReturnData"]["Table1"].ToString());
            yielddata.yieldmachine = JsonConvert.DeserializeObject<List<YieldMachine>>(parm["ReturnData"]["Table2"].ToString());
            yielddata.finalyieldWeek = JsonConvert.DeserializeObject<List<FinalYield>>(parm["ReturnData"]["Table3"].ToString());
            yielddata.finalyieldDay = JsonConvert.DeserializeObject<List<FinalYield>>(parm["ReturnData"]["Table4"].ToString());
            yielddata.rejectrateWeek = JsonConvert.DeserializeObject<List<RejectRate>>(parm["ReturnData"]["Table5"].ToString());
            yielddata.rejectrateDay = JsonConvert.DeserializeObject<List<RejectRate>>(parm["ReturnData"]["Table6"].ToString());

            //yielddata.yield = JsonConvert.DeserializeObject<List<YieldMoude>>(parm["ReturnData"]["ds"].ToString());
            //yielddata.yieldsummary = JsonConvert.DeserializeObject<List<YieldSummary>>(parm["ReturnData"]["ds1"].ToString());
            //yielddata.yieldmachine = JsonConvert.DeserializeObject<List<YieldMachine>>(parm["ReturnData"]["ds2"].ToString());
            return yielddata;
		}
	}

}
       